package mo.leetcode_top;

public class L1578 {

    public int minCost(String s, int[] cost) {
        int count = 0;
        int res = 0;
        char[] chars = s.toCharArray();
        for (int i = 1; i < chars.length; i++) {
            if (chars[i] == chars[i-1]) {
                count++;
            } else {
                if (count > 0) {
                    res += process(i-1, count, cost);
                    count = 0;
                }
            }
            if (i == chars.length - 1 && count > 0) {
                res += process(i, count, cost);
            }
        }
        return res;
    }

    private int process(int i, int count, int[] cost) {
        int max = Integer.MIN_VALUE;
        int sum = 0;
        while (count >= 0) {
            sum += cost[i];
            max = Math.max(max, cost[i]);
            i--;
            count--;
        }
        return sum - max;
    }

    public static void main(String[] args) {
        L1578 tes = new L1578();
        System.out.println(tes.minCost("aabaa", new int[]{1, 2, 3, 4, 1}));
    }

}
